package com.winforsys.yellowbook;

import java.util.ArrayList;

import com.winforsys.yellowbook.DataParser.Member;
import com.winforsys.yellowbook.MyListAdapter.ViewMode;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

public class FaveratesActivity extends Activity {
	
	private ArrayList<MyListItem> mListItems = new ArrayList<MyListItem>();
	private MyListAdapter adapter = null;
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.favarates);
        
        adapter = new MyListAdapter(this, R.layout.listview_custom_row, mListItems);
        
        adapter.setOnItemButtonClickEventListenser(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int pos,
					long id) {
				// TODO Auto-generated method stub
				
				if (adapter.getViewMode() != ViewMode.VM_DELETE)
					return;
				
				String number = mListItems.get(pos).id;
				
				FaverateManager.remove(number);
				mListItems.remove(pos);
				
				adapter.notifyDataSetChanged();
			}
		});
        
        final ListView lvFavorList = (ListView)findViewById(R.id.lvFaverList);
        lvFavorList.setAdapter(adapter);
        
        ImageButton btnEditMode = (ImageButton)findViewById(R.id.btnEditMode);
        
        btnEditMode.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				if (adapter.getViewMode() == ViewMode.VM_DELETE) {
					adapter.setViewMode(ViewMode.VM_REDIRECT);
				} else {
					adapter.setViewMode(ViewMode.VM_DELETE);
				}
				
				adapter.notifyDataSetChanged();
			}
		});
        
        updateFavarList();
    }
    
    public void updateFavarList() {
    	
    	mListItems.clear();
    	
    	String[] ids = FaverateManager.getAll();
    	
    	for (String id : ids) {
    		Member member = AllContactsActivity.GetMemberByID(id);
    		
    		if (member != null) {
    			MyListItem mli = new MyListItem();
    			mli.image = member.image;
    			mli.text = member.name;
    			mli.desc = member.cellphone;
    			mli.id = member.number;
    			
    			mListItems.add(mli);
    		}
    	}
    	
    	adapter.notifyDataSetChanged();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	// TODO Auto-generated method stub
    	
    	menu.add(0, 0, 0, "Contacts").setIcon(android.R.drawable.btn_star_big_off);
    	menu.add(0, 1, 0, "Location").setIcon(android.R.drawable.btn_star_big_off);
    	menu.add(0, 2, 0, "Faverate").setIcon(android.R.drawable.btn_star_big_off);
    	menu.add(0, 3, 0, "Settings").setIcon(android.R.drawable.btn_star_big_off);
    	
    	return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	// TODO Auto-generated method stub
    	switch (item.getItemId()) {
    	case 0:
    	{
    		Intent intent = new Intent(this, AllContactsActivity.class);
    		startActivity(intent);
    	}
    		break;
    	case 1:
    	{
    		Intent intent = new Intent(this, LocationsActivity.class);
    		startActivity(intent);
    	}
    		break;
    	case 2:
    	{
    		Intent intent = new Intent(this, FaveratesActivity.class);
    		startActivity(intent);
    	}
    		break;
    	case 3:
    	{
    		Intent intent = new Intent(this, SettingsActivity.class);
    		startActivity(intent);
    	}
    		break;
    	}
    	
    	return super.onOptionsItemSelected(item);
    }
}
